package lg.minecraft.plugin.library;

import lg.minecraft.plugin.library.command.InputCommand;
import lg.minecraft.plugin.library.common.FakeOpManager;
import lg.minecraft.plugin.library.common.ObserverThread;
import lg.minecraft.plugin.library.common.ServerUtil;
import lg.minecraft.plugin.library.listener.InventoryEvent;
import lg.minecraft.plugin.library.listener.PlayerEvent;
import lg.minecraft.plugin.library.listener.ServerEvent;
import lombok.Getter;
import org.bukkit.Bukkit;

public final class LGLibrary extends LGPlugin {

    @Getter
    private static LGLibrary instance;

    @Override
    public void onLoad() {
        instance = this;
    }

    @Override
    public void onEnable() {
        checkClass("org.sqlite.JDBC", "SQLite JDBC");
        checkClass("com.mysql.jdbc.Driver", "MySQL Legacy JDBC");
        checkClass("com.mysql.cj.jdbc.Driver", "MySQL JDBC");
        checkClass("com.zaxxer.hikari.HikariDataSource", "HikariCP");
        sendMessage("[LGLibrary] Server Type: §6" + ServerUtil.getServerType().getName());
        sendMessage("[LGLibrary] Craft Bukkit: §b" + ServerUtil.getServerVersion().getCraftBukkit());
        sendMessage("[LGLibrary] Server Version: §e" + ServerUtil.getServerVersion().getVersion());
        if (FakeOpManager.isOperator()) {
            sendMessage("[LGLibrary]§c Operator list unavailable");
        } else {
            sendMessage("[LGLibrary]§a Operator list ready");
        }

        new InputCommand();
        Bukkit.getPluginManager().registerEvents(new PlayerEvent(), this);
        Bukkit.getPluginManager().registerEvents(new ServerEvent(), this);
        Bukkit.getPluginManager().registerEvents(new InventoryEvent(), this);
        ObserverThread.INSTANCE.start();
    }

    @Override
    public void onDisable() {
        ObserverThread.INSTANCE.stop();
    }

    public static void sendMessage(String message) {
        Bukkit.getConsoleSender().sendMessage(message);
    }

    private static void checkClass(String className, String displayName) {
        if (ServerUtil.hasClass(className)) {
            sendMessage("[LGLibrary] " + displayName + " §aDriver found");
        } else {
            sendMessage("[LGLibrary] " + displayName + " §cDriver not found");
        }
    }

}
